package com.shizhanzhe.zxbapp.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppCompatActivity {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_ps)
    EditText etPs;
    @BindView(R.id.et_vercode)
    EditText etVercode;
    @BindView(R.id.tv_vercode)
    TextView tvVercode;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.ll_qu_reg)
    LinearLayout llQuReg;
    @BindView(R.id.et_sx_id)
    EditText etSxId;
    private CountDownTimer time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .transparentBar()
                .init();
    }

    private boolean check(String phone, String ps, String ver) {
        if (Util.isMobile(phone)) {
            if (!"".equals(ps)) {
                if (!"".equals(ver)) {
                    return true;
                } else {
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return false;
                }
            } else {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else {
            Toast.makeText(this, "手机号输入有误", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    @OnClick({R.id.iv_close, R.id.tv_vercode, R.id.tv_login, R.id.ll_qu_reg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.tv_vercode:
                if (!"".equals(etPhone.getText().toString())) {
                    if (Util.isMobile(etPhone.getText().toString())) {
                        reCode(etPhone.getText().toString());
                        time = new CountDownTimer(60000, 1000) {
                            @SuppressLint("SetTextI18n")
                            @Override
                            public void onTick(long millisUntilFinished) {
                                tvVercode.setClickable(false);
                                tvVercode.setText(millisUntilFinished / 1000 + "秒");
                            }

                            @Override
                            public void onFinish() {
                                tvVercode.setText("重新验证");
                                tvVercode.setClickable(true);
                            }
                        };
                        time.start();
                    } else {
                        Toast.makeText(this, "手机号输入有误", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_login:
                if (check(etPhone.getText().toString(), etPs.getText().toString(), etVercode.getText().toString())) {
                    //注册接口
                    register(etPhone.getText().toString(), etPs.getText().toString(), etVercode.getText().toString(), etSxId.getText().toString());
                }
                break;
            case R.id.ll_qu_reg:
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }

    private void reCode(String phone) {
        String sign=Util.encode(Util.encode(phone+"zhuanxiaobao")+"zhuanxiaobao");
        OkHttpDownloadJsonUtil.downloadJson(this, Path.sendPhoneCode + "?phone=" + phone+ "&sign=" +sign+"&type=1",
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                Log.d("jsonddd", json);
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        if (jsonObject.has("info")) {
                            Toast.makeText(RegisterActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(RegisterActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //注册接口
    private void register(String phone, String ps, String vercode, String pid) {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.register + "?phone=" + phone + "&code=" + vercode + "&password=" + ps + "&pid=" + pid+ "&device=" + Util.androidId(this)+ "&channel=" + UserBean.qudao,
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        if (!"1".equals(json) && !"2".equals(json)) {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                if (jsonObject.has("info")) {
                                    Toast.makeText(RegisterActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                                }
                                if (jsonObject.has("status")) {
                                    if (jsonObject.getInt("status") == 1) {
                                        finish();
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }else {
                            Toast.makeText(RegisterActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
